var target_marker;
var directionOverlay=null;
$(function(){
	$(".ksr .reset-map").click(function(){
		mapUtil.init();
		mapUtil.cleanUpDirections(directionOverlay,"ksr-map-direction");
	});
	$(".ksr form[name='ksr-get-direction-form']").validate({
		errorContainer:	$(this).find("div.errContainer" ),
		messages:{
			from:"Please input start",
			to:"Please input destination"
		},
		submitHandler:function(form){
			$("#getDirectionFormOverlay").modal("hide");
			mapUtil.cleanUpDirections(directionOverlay,"ksr-map-direction");
			mapUtil.getDrivingDirections(mapUtil.mapInstance,"ksr-map-direction",$(form).find("input[name='from']").val(),$(form).find("input[name='to']").val(),KSR.Office.getDirectionsCallback)
			return false;
		}
	});
	
	//on get direction overlay, change from/to
	$(".ksr form[name='ksr-get-direction-form'] input[name='direction']").change(function(){
		if ($(this).val()=="from"){
			var fromAddr=$(".ksr form[name='ksr-get-direction-form'] input[name='to']").val();
			var toAddr=$(".ksr form[name='ksr-get-direction-form'] input[name='address']").val();
		} else {
			var fromAddr=$(".ksr form[name='ksr-get-direction-form'] input[name='address']").val();
			var toAddr=$(".ksr form[name='ksr-get-direction-form'] input[name='from']").val();
		}
		$(".ksr form[name='ksr-get-direction-form'] input[name='from']").val(fromAddr);
		$(".ksr form[name='ksr-get-direction-form'] input[name='to']").val(toAddr);
		$(this).parents("form").find("label.error").hide();
	});
	
	
});
KSR.Office={
	init:function(){
		var self=this;
		$(".ksr .customer-review a.review-distribution").click(function(){
			$(".ksr .customer-review .rating-distribution").toggle("fast");
		})
		
		//toggle write a review form
		$(".ksr .write-review-button").click(function(){
			$(".ksr .write-a-review-form-wrapper").toggle("fast");
		})
		
		$(".ksr form[name='ksr-customer-review-form'] .rating .star").bind("click",function(){
			$(this).parents(".rating").find(".star").removeClass("select");
			$(this).addClass("select");
			$(this).parents("form").find("input[name='rating']").val($(this).attr("rating"));
			if ($(this).parents("form").find("div.errContainer .error:visible").length>0){
				$(this).parents("form").validate().form();
			}
			console.log("rating="+$(this).attr("rating"));
		});
		
		$(".ksr form[name='ksr-customer-review-form']").each(function(){
			var thisForm=this;
			$(this).validate({
				ignore:"",
				errorContainer:	$(this).find("div.errContainer" ),
				messages:{
					title:"Please input review title.",
					rating:"Please select rating.",
					content:"Please input your comment."
				},
				submitHandler:function(form){
					if($(form).hasClass("create")&&($(form).attr("user-login")=="false")){//open login modal
						console.log("user creates a new review and not logged in");
						$("#customerLoginSigninOverlay").modal("show");
						$('#customerLoginSigninOverlay').on('hidden.bs.modal', function () {
							if($(this).attr("user-login")=="true"){
								form.submit();
							}
						})
						return false;
					} else {
						console.log("edit a review or user is logged in");
						form.submit();
					}
				}
			});
		})
		
		//edit user review
//		("#customerReviewOverlay").modal();
		$(".ksr .review a.edit-review").click(function(){
			var reviewData = $(this).attr("href");
			$("#customerReviewOverlay form[name='ksr-customer-review-form'] input[name='office_id']").val($(reviewData+" span.office-id").text());
			$("#customerReviewOverlay form[name='ksr-customer-review-form'] input[name='id']").val($(reviewData+" span.review-id").text());
			$("#customerReviewOverlay form[name='ksr-customer-review-form'] input[name='title']").val($(reviewData+" span.review-title").text());
			$("#customerReviewOverlay form[name='ksr-customer-review-form'] textarea[name='content']").val($(reviewData+" span.review-content").text());
			$("#customerReviewOverlay form[name='ksr-customer-review-form'] input[name='rating']").val($(reviewData+" span.review-rating").text());
			$("#customerReviewOverlay form[name='ksr-customer-review-form'] span.rating .star").each(function(){
				if ($(this).attr("rating")==$(reviewData+" span.review-rating").text()){
					$(this).addClass("select");
				}
			})
			$("#customerReviewOverlay form[name='ksr-customer-review-form']").validate().form();
			$("#customerReviewOverlay").modal("show");
			return false;
		});
		$("#customerReviewOverlay").on('hidden.bs.modal', function () {
			$("#customerReviewOverlay form[name='ksr-customer-review-form']")[0].reset();
			$("#customerReviewOverlay form[name='ksr-customer-review-form'] span.rating .star").removeClass("select");
			$("#customerReviewOverlay form[name='ksr-customer-review-form'] label.error").hide();
		});
		
		//customerLoginSigninOverlay switch login/sign-in mode
		$("#customerLoginSigninOverlay button.create-an-account").click(function(){
			$("#customerLoginSigninOverlay .login-mode").hide("fast");
			$("#customerLoginSigninOverlay .signin-mode").show("fast");
		})
		$("#customerLoginSigninOverlay button.log-in-now").click(function(){
			$("#customerLoginSigninOverlay .signin-mode").hide("fast");
			$("#customerLoginSigninOverlay .login-mode").show("fast");
		})
	},
	onMapLoadCallback:function(map){
			place.markerImage=new google.maps.MarkerImage(
					mapUtil.settings.centerMarkerImage.pinUrl,
					null,
					null,
					new google.maps.Point(mapUtil.settings.centerMarkerImage.anchor.x,mapUtil.settings.centerMarkerImage.anchor.y)
			);
			place.shadowImage=new google.maps.MarkerImage(
					mapUtil.settings.centerMarkerImage.shadowUrl,
					null,
					null,
					new google.maps.Point(mapUtil.settings.centerMarkerImage.anchor.x,mapUtil.settings.centerMarkerImage.anchor.y)
			);
			place.hoverImage=place.markerImage;			
			target_marker = mapUtil.addMarker(mapUtil.mapInstance,place);
//			markers.push(mapUtil.addMarker(mapUtil.mapInstance,place));
			
			mapUtil.mapInstance.setCenter(new google.maps.LatLng(place.lat,place.lng))		
			mapUtil.mapInstance.setZoom(15);
	},
	getDirectionsCallback:function(directionDisplay,status){
		if (status == google.maps.DirectionsStatus.OK) {
//			target_marker.setMap(null);
			directionOverlay=directionDisplay;
			$("#ksr-map-direction").show();
			$("#error-message-container").hide();
		}else {
			if (status == google.maps.DirectionsStatus.NOT_FOUND) {
				$("#error-message-container").html("<b>We're sorry.</b> We are unable to match one of the addresses below. Please review both addresses and make any necessary changes.");
			} else {
				$("#error-message-container").html("Error occurred: " + status);
				if (typeof console != "undefined") 
					console.error("error occurred: " + status);
			}
			$("#ksr-map-direction").hide()
			$("#error-message-container").show()
		}	
	}
	
}